﻿import { _decorator, Component, Node, input, Input, EventKeyboard, KeyCode, game, loader, assetManager, Texture2D, sys, TextAsset, NodeEventType, EventTouch, UITransform, view, Vec2, Vec3, Sprite, Color, Label } from 'cc';
import { ComBase } from '../../base/ComBase';
import { Defines } from '../Defines';
import { GlobalVar } from '../GlobalVar';
import { ResourceLoader } from '../ResourceLoader';
import { ResourcesPathDefine } from '../ResourcesPathDefine';
import { SysMgr } from '../SysMgr';
import { CodeReoladTool } from '../../tools/CodeReoladTool';
import { BubbleTipDlgCom } from '../../dlg/dlgcom/BubbleTipDlgCom';
import { Functor } from '../Functor';
import { ClickFunctor } from '../ClickFunctor';
import { DlgBase } from '../../base/DlgBase';


const { ccclass, property } = _decorator;

@ccclass('KeyCom')
export class KeyCom extends ComBase {
    ctrlDown = false;
    ctrlFuncDict: Map<number, string> = new Map([
        [KeyCode.KEY_D, "reload"],
    ])
    funcDict: Map<number, string> = new Map([

    ])
    onLoad() {
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
       
    }
    async reload() {
        if (!GlobalVar.isTest)
            return;
        CodeReoladTool.reload()
        await BubbleTipDlgCom.createTip("重载完成")
    }

    onMouseClick(event: EventTouch) {

        let canvasDesignSize = GlobalVar.canvas.getComponent(UITransform).contentSize;
        let canvasSize = view.getCanvasSize();
        let visibleSize = view.getVisibleSizeInPixel()
        let pos = event.getLocation();

        let ratioX = canvasDesignSize.x / visibleSize.x
        let ratioY = canvasDesignSize.y / visibleSize.y
        let startPos = new Vec2(ratioX * pos.x - ratioX * canvasSize.x / 2, ratioY * pos.y - ratioY * canvasSize.y / 2);
        let touchWorldPos = new Vec3();
        Vec3.transformMat4(touchWorldPos, new Vec3(startPos.x, startPos.y, 0), GlobalVar.clickListenerNode.getWorldMatrix());



        let parent = GlobalVar.canvas
        let len = parent.children.length - 2;
        let result = null;
        for (let i = len - 1; i >= 0; i--) {
            let node = parent.children[i]
            if (!node.active)
                continue
            result = this.findClickNode(node, touchWorldPos)
            if (result != null)
                break;
        }
        if (result == null) {
            BubbleTipDlgCom.createTip("未能找到界面代码")
            return;
        }

        let dlgCom = null
        for (let i = 0; i < 1200; i++) {//防止死循环 1200足够了
            dlgCom = result.getComponent(DlgBase)
            if (dlgCom != null)
                break;
            result = result.parent
        }
        BubbleTipDlgCom.createTip("界面：" + dlgCom.constructor.name)
        console.log(dlgCom)
        navigator.clipboard.writeText(dlgCom.constructor.name)
    }

    findClickNode(parent: Node, touchPos: Vec3) {
        if (!parent.active)
            return null;
        let len = parent.children.length;
        for (let i = len - 1; i >= 0; i--) {
            let node = parent.children[i]
            let result = this.findClickNode(node, touchPos);
            if (result != null)
                return result;
        }


        if (parent.getComponent(Sprite) == null && parent.getComponent(Label) == null)
            return null;

        let pos = parent.worldPosition;
        let contentSize = parent.getComponent(UITransform).contentSize
        if (touchPos.x > pos.x + contentSize.x / 2)
            return null;
        if (touchPos.x < pos.x - contentSize.x / 2)
            return null;
        if (touchPos.y > pos.y + contentSize.y / 2)
            return null;
        if (touchPos.y < pos.y - contentSize.y / 2)
            return null;
        return parent;
    }



    async onKeyDown(event: EventKeyboard) {
        if (event.keyCode == KeyCode.CTRL_LEFT) {
            this.ctrlDown = true;
            if (GlobalVar.isTest) {
                GlobalVar.clickListenerNode.active = true;
                GlobalVar.clickListenerNode.on(NodeEventType.TOUCH_START, ClickFunctor.getFunc(this, "onMouseClick"), this);
            }
        }

        if (this.ctrlDown) {
            if (this.ctrlFuncDict.has(event.keyCode)) {
                let funcName = this.ctrlFuncDict.get(event.keyCode)
                await this[funcName]();
            }
        }

       
        if (this.funcDict.has(event.keyCode)) {
            let funcName = this.funcDict.get(event.keyCode)
            await this[funcName]();
        }
    }

    onKeyUp(event: EventKeyboard) {
        if (event.keyCode == KeyCode.CTRL_LEFT) {
            this.ctrlDown = false;
            if (GlobalVar.isTest) {
                GlobalVar.clickListenerNode.active = false;
                GlobalVar.clickListenerNode.off(NodeEventType.TOUCH_START);
            }
        }
    }
}

